package com.hqyj.springBoot.test.dao;

import com.hqyj.springBoot.test.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

/**
 * @author YangGuo
 * @version 1.0
 * @date 2021/8/21 15:07
 */
@Repository
public interface StudentRepository extends JpaRepository<Student,Integer>, JpaSpecificationExecutor<Student> {

    /**
     * 使用官方提供的搭配模式设计接口
     * @param name
     * @param email
     * @return
     */
    Student findFirstByNameAndEmail(String name, String email);

    /**
     * 使用HQL创建接口
     * @param id
     * @return
     */
    @Query(value = "from Student where id = :id")
    Student getStudentById(@Param("id") int id);

    /**
     * 使用SQL创建接口
     * @param id
     * @return
     */
    @Query(nativeQuery=true, value = "select * from test_student where id = :id")
    Student getStudentByIdForSql(@Param("id") int id);

    /**
     * 修改语句
     * @param student
     */
    @Query(nativeQuery=true, value = "insert into test_student (name, email, age) " +
            "values(:#{#student.name}, :#{#student.email}, :#{#student.age})")
    @Modifying
    void insertStudent(@Param("student") Student student);
}